package com.feng.gameproj;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.View;

public class DropSpreadView extends View implements Runnable 
{
	private Bitmap pict = null;
	
	private int width = 0;
	private int height = 0;
	
	private short[] buff1 = null;
	private short[] buff2 = null;
	private int[] bitmap1 = null;
	private int[] bitmap2 = null;

	public DropSpreadView(Context context) 
	{
		super(context);
		
		this.pict = BitmapFactory.decodeResource(getResources(), R.drawable.drop);
		this.width = this.pict.getWidth();
		this.height = this.pict.getHeight();
		
		this.buff1 = new short [this.width * this.height];
		this.buff2 = new short [this.width * this.height];
		
		this.bitmap1 = new int [this.width * this.height];
		this.bitmap2 = new int [this.width * this.height];
		
		this.pict.setPixels(this.bitmap1, 0, this.width, 0, 0, this.width, this.height);
		
		new Thread (this).start();
	}

	@Override
	protected void onDraw(Canvas canvas) 
	{
		super.onDraw(canvas);
		
		//canvas.drawBitmap(this.pict, 0, 0, null);
		canvas.drawBitmap(this.bitmap2, 0, this.width, 
				0, 0, this.width, this.height, false, null);
	}

	@Override
	public void run() 
	{
		while (!Thread.currentThread().isInterrupted())
		{
			try 
			{
				Thread.sleep(50);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Thread.currentThread().interrupt();
			}
		}
		//this.dropStone(this.width/2, this.height/2, 10, 5);
		this.rappleSpread();
		this.render();
		postInvalidate ();
	}
	
	private void dropStone (int x, int y, int stoneR, int stoneW)
	{
		for (int posx=x-stoneR; posx < x+stoneR; ++posx)
		{
			for (int posy=y-stoneR; posy < y+stoneR; ++posy)
			{
				if ((posx-x)*(posx-x) + (posy-y)*(posy-y) < stoneR*stoneR)
				{
					buff1[this.width*posy + posx] = (short)-stoneW;
				}
			}
		}
	}
	
	private void rappleSpread ()
	{
		for (int i=this.width; i<this.width*this.height - this.width; ++i)
		{
			buff2[i] = (short) (((buff1[i-1] + buff1[i+1] + 
					buff1[i-this.width] + buff1[i+this.width]) >> 1) - buff2[i]);
			buff2[i] -= buff2[i] >> 5; 
		}
		
		short[] ptmp = buff1;
		buff1 = buff2;
		buff2 = ptmp;
	}
	
	private void render ()
	{
		int xoffset, yoffset;
		int k = this.width;
		
		for (int i=1; i<this.height-1; ++i)
		{
			for (int j=0; j<this.width; ++j)
			{
				xoffset = buff1[k-1] - buff1[k+1];
				yoffset = buff1[k-this.width] - buff1[k+this.width];
				
				if ((i + yoffset) < 0)
				{
					++ k;
					continue;
				}
				if ((i + yoffset) > this.height)
				{
					++ k;
					continue;
				}
				if ((j + xoffset) < 0)
				{
					++ k;
					continue;
				}
				if ((j + xoffset) > this.width)
				{
					++ k;
					continue;
				}
				
				int pos1, pos2;
				pos1 = this.width * (i+yoffset) + (j+xoffset);
				pos2 = this.width * i + j;
				bitmap2[pos2 ++] = bitmap1[pos1 ++];
				++ k;
			}
		}
	}

}
